package com.bluegalaxy.nhb.snapi.controller.api.ck;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.nhb.snservice.domain.constant.AppConstant;
import com.bluegalaxy.nhb.snservice.domain.vo.api.R;
import com.bluegalaxy.nhb.snservice.service.ck.UserGroupService;
import com.bluegalaxy.nhb.snservice.service.ck.UserTagsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户标签
 */
@RestController
@RequestMapping(AppConstant.APPLICATION_NAME+"/user/tags")
@Api(value = "用户标签", tags = "用户标签API")
public class UserTagsController {
    private static Log log = LogFactory.getLog(UserTagsController.class);

    @Autowired
    private UserTagsService userTagsService;


    @ResponseBody
    @PostMapping("/findCount")
    @ApiOperation(value = "根据条件查询人数", notes = "根据条件查询人数")
    public R findTagsCount(@RequestBody JSONObject  paramVO) {
        log.info("user/tags findCount:"+JSONObject.toJSONString(paramVO));
        return userTagsService.countUsers(paramVO);
    }

    @ResponseBody
    @PostMapping("/findUsers")
    @ApiOperation(value = "根据条件查询userList", notes = "根据条件查询userList")
    public R findTagsUsers(@RequestBody JSONObject  paramVO) {
        log.info("user/tags findUsers:"+JSONObject.toJSONString(paramVO));
        return userTagsService.findUsersById(paramVO);
    }

    @ResponseBody
    @GetMapping("/find/{tableId}")
    @ApiOperation(value = "根据tableId查询userList", notes = "根据tableId查询userList")
    public R findUsersById(@PathVariable("tableId") String tableId) {
        List<String> users = new ArrayList<>();

        return R.data(users);
    }

    @ResponseBody
    @PostMapping("/createUsers")
    @ApiOperation(value = "根据条件创建用户分群", notes = "根据条件创建用户分群")
    public R createTagsUsers(@RequestBody JSONObject  paramVO) {
        log.info("user/tags createUsers:"+JSONObject.toJSONString(paramVO));
        return userTagsService.createUsers(paramVO);
    }

    @ResponseBody
    @PostMapping("/findTagsByUser")
    @ApiOperation(value = "获取用户所属标签", notes = "获取用户所属标签")
    public R findTagsByUser(@RequestBody JSONObject  paramVO) {
        log.info("user/tags findTagsByUser:"+JSONObject.toJSONString(paramVO));

        List<String> tableList = paramVO.getJSONArray("tableList").toJavaList(String.class);
        String distinctId = paramVO.getString("distinctId");
        return userTagsService.findTagsByUser(tableList,distinctId);
    }


}
